package com.see.server.command;

import java.util.HashMap;

import com.see.domain.Order;
import com.see.domain.OrderServise;
import com.see.domain.Protocol;
import com.see.exception.MessegeFeedException;
import com.see.exception.UserNotAuthenticatException;
import com.see.server.SocketConnection;
import com.see.servise.SEEServise;

public class SendOrderCommand implements Command {
	
		
	private SocketConnection socketConnection;
	private HashMap<String, String> paramMap;
	
	public SendOrderCommand(SocketConnection socketConnection, HashMap<String, String> paramMap) {
		this.socketConnection = socketConnection;
		this.paramMap = paramMap;
	}

	@Override
	public String execute() throws UserNotAuthenticatException, MessegeFeedException {
		if(!socketConnection.isAuthenticated())
			throw new UserNotAuthenticatException(Protocol.errormsg(Protocol.SENDORDED_FIELD, Protocol.ERROR_USERNOTAUTH));
		
		Order neworder = OrderServise.createOrderFromMessege(paramMap, true);
		
		SEEServise instance = SEEServise.getInstance();
		
		neworder.setId(instance.orderCounter.incrementAndGet());
		instance.addToOrderQueue(neworder);	
		
		return Protocol.successmsg(Protocol.SENDORDED_FIELD, Protocol.ORDERID + neworder.getId());
	}

	
}
